﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using Things.Common;
using LTP.Accounts.Bus;
namespace Things.Web.GoodsImages
{
    public partial class Add : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.Params["ID"] != null && Request.Params["ID"].Trim() != "")
                {
                    Guid goodsID = Guid.Empty;
                    Guid.TryParse(Request.Params["ID"], out goodsID);
                    if (goodsID != Guid.Empty)
                    {

                        btnGoCrops.Visible = true;
                    }
                }
                else
                {

                    btnGoCrops.Visible = false;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {

            string strErr = "";

            Guid goodsID = Guid.Empty;
            Guid.TryParse(Request.Params["ID"], out goodsID);
            if (goodsID == Guid.Empty)
            {
                strErr += "商品ID有误！\\n";
            }
            if (!PageValidate.IsNumber(txtImageOrder.Text))
            {
                strErr += "图片顺序格式错误！\\n";
            }
            PageUtility pu = new PageUtility();
            string imageURL = pu.UploadFile(FileUpload1);
            if (string.IsNullOrEmpty(imageURL) == true)
            {
                strErr += "图片上传路径不正确！\\n";
            }

            if (strErr != "")
            {
                MessageBox.Show(this, strErr);
                return;
            }

            int ImageOrder = int.Parse(this.txtImageOrder.Text);
            DateTime CreatedDate = DateTime.Now;
            string ImageURL = imageURL;

            Things.Model.GoodsImages model = new Things.Model.GoodsImages();
            model.GoodsID = goodsID;
            model.ImageOrder = ImageOrder;
            model.CreatedDate = CreatedDate;
            model.ImageURL = ImageURL;

            Things.BLL.GoodsImages bll = new Things.BLL.GoodsImages();
            bll.Add(model);
            Things.Common.MessageBox.ShowAndRedirect(this, "保存成功！", "list.aspx?ID="+goodsID.ToString());

        }


        public void btnCancle_Click(object sender, EventArgs e)
        {
            Guid goodsID = Guid.Empty;
            Guid.TryParse(Request.Params["ID"], out goodsID);
            Response.Redirect("list.aspx?ID=" + goodsID.ToString());
        }

        protected void btnGoCrops_Click(object sender, EventArgs e)
        {
            Response.Redirect("../Goods/list.aspx");
        }
    }
}
